package com.uade.services;


import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.uade.facade.Facade;

public class ServiceLocator {
	public static Facade getRemoteFacade() throws NamingException {
		final String appName = "PortalBackendEar";
		final String moduleName = "PortalBackendEarEJB";
		final String sessionBeanName = "FacadeBean";
		final String viewClassName = Facade.class.getName();
		Properties jndiProps = new Properties();
		jndiProps.put(Context.INITIAL_CONTEXT_FACTORY,
				"org.jboss.naming.remote.client.InitialContextFactory");
		jndiProps.put(Context.PROVIDER_URL, "remote://127.0.0.1:4447");
		// username
		jndiProps.put(Context.SECURITY_PRINCIPAL, "test");
		// password
		jndiProps.put(Context.SECURITY_CREDENTIALS, "test123");
		// This is an important property to set if you want to do EJB
		// invocations via the remote naming project
		jndiProps.put("jboss.naming.client.ejb.context", true);
		// create context passing these properties
		Context context = new InitialContext(jndiProps);

		// lookup the bean facade
		Facade facade = (Facade) context.lookup(appName + "/" + moduleName
				+ "/" + sessionBeanName + "!" + viewClassName);

		return facade;
	}
	
	public static Facade getRemoteFacadeB() throws NamingException {
		Facade facade = null;

		/*
		The app name is the EAR name of the deployed EJB without .ear suffix.
		Since we haven't deployed the application as a .ear,
		the app name for us will be an empty string
		*/

		String appName = "PortalBackendEar";
		 
        /* The module name is the JAR name of the deployed EJB
        without the .jar suffix.
        */
        String moduleName = "PortalBackendEarEJB";
		 
		/*AS7 allows each deployment to have an (optional) distinct name.
		This can be an empty string if distinct name is not specified.
		*/
        String distinctName = "";
 
        // The EJB bean implementation class name
        String beanName = "FacadeBean";
 
        // Fully qualified remote interface name
        final String interfaceName = Facade.class.getName();
 
        // Create a look up string name
        String name = "ejb:" + appName + "/" + moduleName + "/" +
            distinctName    + "/" + beanName + "!" + interfaceName;
				
		Hashtable props = new Hashtable();

		props.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming");
		InitialContext ic = new InitialContext(props);

		facade = (Facade)ic.lookup(name);

		return facade;
	}
}
